package homework;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @classname:demo06
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/16 20:27
 * @version: 0.1
 * @since:1.8
 */
public class demo06 {
    public static void main(String[] args) {
        String sourcePath = "day16/src/d.txt";
        try(
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(sourcePath));
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("day16/src/e.txt"));
                ){

            byte[] bytes = new byte[1024];
            int len = 0;
            String str = "";
        while ((len=in.read(bytes))!=-1){
            str = new String(bytes,0,len);
            String distins = distins(str);
            byte[] bytes1 = distins.getBytes();
            out.write(bytes1);

        }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static String distins(String string) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < string.length(); i++) {
            char charAt = string.charAt(i);
            String s = String.valueOf(charAt);
            if(!list.contains(s)){
                list.add(s);
            }
        }
        Collections.sort(list);
        StringBuilder sb = new StringBuilder();
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            sb.append(next);
        }
       return sb.toString();
    }
}
